/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "SettingsDialog.h"

SettingsDialog::SettingsDialog(HINSTANCE hInstance) throw(): Dialog(hInstance, TEXT("Settings"), CS_OKCANCEL), m_inputDevice(0), m_outputDevice(0), m_hasSettings(readSettings()) {
    CreateWindow(TEXT("STATIC"), TEXT("Input device:"), WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX, widthToPixels(4), heightToPixels(4), widthToPixels(width() - 8), heightToPixels(8), hWnd(), 0, hInstance, 0);
    m_hInputDev = CreateWindow(TEXT("COMBOBOX"), 0, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS, widthToPixels(4), heightToPixels(16), widthToPixels(width() - 8), heightToPixels(64), hWnd(), 0, hInstance, 0);
    for(unsigned int i = 0; i < m_devFac.numInputDevices(); i++) {
        SendMessage(m_hInputDev, CB_ADDSTRING, 0, LPARAM(m_devFac.inputDeviceName(i)));
    }
    SendMessage(m_hInputDev, CB_SETCURSEL, m_inputDevice, 0);

    CreateWindow(TEXT("STATIC"), TEXT("Output device:"), WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX, widthToPixels(4), heightToPixels(36), widthToPixels(width() - 8), heightToPixels(8), hWnd(), 0, hInstance, 0);
    m_hOutputDev = CreateWindow(TEXT("COMBOBOX"), 0, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS, widthToPixels(4), heightToPixels(48), widthToPixels(width() - 8), heightToPixels(64), hWnd(), 0, hInstance, 0);
    for(unsigned int i = 0; i < m_devFac.numOutputDevices(); i++) {
        SendMessage(m_hOutputDev, CB_ADDSTRING, 0, LPARAM(m_devFac.outputDeviceName(i)));
    }
    SendMessage(m_hOutputDev, CB_SETCURSEL, m_outputDevice, 0);
}

bool SettingsDialog::hasSettings() const throw() {
    return(m_hasSettings);
}

InputDevice *SettingsDialog::instantiateCurrentInputDevice() throw() {
    return(m_devFac.instantiateInputDevice(m_inputDevice));
}

OutputDevice *SettingsDialog::instantiateCurrentOutputDevice() throw() {
    return(m_devFac.instantiateOutputDevice(m_outputDevice));
}

bool SettingsDialog::readSettings() throw() {
    HKEY hKey;
    if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("\\Software\\Digital Audio Mobile"), 0, 0, &hKey) != ERROR_SUCCESS) {
        return(false);
    }

    DWORD type, size = sizeof(DWORD);
    RegQueryValueEx(hKey, TEXT("InputDevice"), 0, &type, LPBYTE(&m_inputDevice), &size);
    RegQueryValueEx(hKey, TEXT("OutputDevice"), 0, &type, LPBYTE(&m_outputDevice), &size);

    RegCloseKey(hKey);

    return(true);
}

void SettingsDialog::writeSettings() throw() {
    HKEY hKey;
    if(RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("\\Software\\Digital Audio Mobile"), 0, 0, REG_OPTION_NON_VOLATILE, 0, 0, &hKey, 0) == ERROR_SUCCESS) {
        DWORD inputDevice = SendMessage(m_hInputDev, CB_GETCURSEL, 0, 0);
        RegSetValueEx(hKey, TEXT("InputDevice"), 0, REG_DWORD, reinterpret_cast<const BYTE *>(&inputDevice), sizeof(DWORD));

        DWORD outputDevice = SendMessage(m_hOutputDev, CB_GETCURSEL, 0, 0);
        RegSetValueEx(hKey, TEXT("OutputDevice"), 0, REG_DWORD, reinterpret_cast<const BYTE *>(&outputDevice), sizeof(DWORD));
    }
}

bool SettingsDialog::close(CloseCommand cc) throw() {
    if(cc == CC_OK) {
        writeSettings();
        m_hasSettings = true;
    }

    return(true);
}

void SettingsDialog::controlNotification(HWND hWnd, WPARAM notification) throw() {
    if(hWnd == m_hInputDev && notification == CBN_SELCHANGE) {
        m_inputDevice = SendMessage(m_hInputDev, CB_GETCURSEL, 0, 0);
    }
    else if(hWnd == m_hOutputDev && notification == CBN_SELCHANGE) {
        m_outputDevice = SendMessage(m_hOutputDev, CB_GETCURSEL, 0, 0);
    }
}

void SettingsDialog::menuNotification(unsigned int itemId) throw() {
}
